import datetime
from rest_framework import permissions


class MainOrderIsOwnerOrReadOnly(permissions.BasePermission):
    """
    当前访问用户 和当前对象用户是同一个用户
    """

    # 请求权限
    def has_object_permission(self, request, view, obj):
        # 任何请求都允许读取权限，
        # 所以我们总是允许GET，HEAD或OPTIONS 请求.
        if request.method in permissions.SAFE_METHODS:
            return True
        return obj.supply_order.user == request.user


class IsOwnerOrReadOnly(permissions.BasePermission):
    """
    当前访问用户 和当前对象用户是同一个用户
    """

    # 请求权限
    def has_object_permission(self, request, view, obj):
        # 任何请求都允许读取权限，
        # 所以我们总是允许GET，HEAD或OPTIONS 请求.
        if request.method in permissions.SAFE_METHODS:
            return True
        return obj.user == request.user


class IsOwner(permissions.BasePermission):
    """
    当前访问用户 和当前对象用户是同一个用户
    """

    # 请求权限
    def has_object_permission(self, request, view, obj):
        # 任何请求都允许读取权限，
        # 所以我们总是允许GET，HEAD或OPTIONS 请求.
        if request.method in permissions.SAFE_METHODS:
            return True
        elif obj == request.user:
            return True
        elif hasattr(obj, "username"):
            return getattr(obj, "username") == request.user.username
        else:
            return False


class IsCompany(permissions.BasePermission):
    """
    判断是哪个公司的用户
    """

    def has_permission(self, request, view):
        if request.user.company:
            return True


class CzbOwner(permissions.BasePermission):
    # 车主邦加油使用权限，至于是会员或者购买了加油权益礼包才能使用
    def has_permission(self, request, view):
        """
        Return `True` if permission is granted, `False` otherwise.
        """
        pay_order_model = request.data.get("pay_order_model")
        if pay_order_model == "czb":
            supply_id = request.data.get("supply_id")
            from apps.czb.models import Orders
            order_type = Orders.objects.get(id=supply_id).order_type
            if order_type == 2:  # 如果是购买加油权益
                return True
            else:
                if request.user.mobile == "a":
                    setattr(self, "message", "buy_permission")
                elif request.user.oil_flag and request.user.oil_flag_expiry_date and datetime.date.today() <= request.user.oil_flag_expiry_date:
                    return True
                else:
                    setattr(self, "message", "buy_permission")
        else:
            return True


class AddressPermission(permissions.BasePermission):
    # 车主邦加油使用权限，至于是会员或者购买了加油权益礼包才能使用
    def has_permission(self, request, view):
        """
        Return `True` if permission is granted, `False` otherwise.
        """
        province = request.data.get("province")
        if province:
            return True
        else:
            user = request.user
            user_receiver = user.user_receiver_info.filter(default=True)
            if user_receiver.exists():
                return True
            else:
                setattr(self, "message", "noAddress")


class BuyGasOwner(permissions.BasePermission):
    # 车主邦加油使用权限，至于是会员或者购买了加油权益礼包才能使用
    def has_permission(self, request, view):
        """
        Return `True` if permission is granted, `False` otherwise.
        """
        if request.user.oil_flag and request.user.oil_flag_expiry_date and datetime.date.today() <= request.user.oil_flag_expiry_date:
            return True
        setattr(self, "message", "您还未购买加油权益")